﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace XLang
{
    /// <summary>
    /// 语法分析器规则
    /// </summary>
    public class ParserRule
    {
        public List<Type> Input { get; private set; }
        public Type Output { get; private set; }
        public List<List<Type>> Condtion { get; private set; }
        public Func<AST[], AST> Create { get; private set; }

        public ParserRule(MethodInfo method)
        {
            this.Input = new List<Type>(method.GetParameters().Select(i => i.ParameterType));
            this.Output = method.ReturnType;
            this.Condtion = new List<List<Type>>();

            var condtions = method.GetCustomAttributes<ReduceCondtionAttribute>();
            foreach (var condtion in condtions)
            {
                this.Condtion.Add(condtion.Condtions);
            }

            this.Create = (args) =>
            {
                var ast = method.Invoke(null, args);
                if (ast == null) return null;

                return ast as AST;
            };
        }
    }
}
